package com.model.action.admin;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.model.action.BaseAction;
import com.model.service.admin.AdminService;
import com.model.util.PageList;
import com.model.vo.Admin;
import com.model.vo.Teacher;

/**
 * 管理员 信息管理
 * @author he
 * 控制层
 */
@Controller
@SuppressWarnings("serial")
public class AdminAction extends BaseAction implements SessionAware{
    
	/*实体参数 声明
	 **********************************************/
	Map session;
	Admin admin;   //实体类
	@Autowired
	AdminService adminService;  //业务层
	String resultView;   //视图页面
    @SuppressWarnings("rawtypes")
	PageList dataList;
	/*相关方法
	 * ********************************************/
    @Override
    public String execute() throws Exception {
    	if(admin==null){
    		admin = new Admin();
    		admin.setId((Integer) session.get("nameId"));
    	}
    	admin = adminService.QueryById(Admin.class, admin.getId());
    	resultView ="admin/password.jsp";
    	return "edit";
    }
	/**
	 * 保存
	 * @return
	 */
	public String save(){
		if(admin!=null){
			adminService.save(admin);
		}
		return SUCCESS;
	}
	/**
	 * 更新
	 * @return
	 */
    public String update(){
    	if(admin!=null){
			adminService.update(admin);
		}
    	resultView ="/admin/sys.action";
		return "update";
	}
    /**
     * 删除
     * @return
     */
   public String delete(){
	   if(admin!=null){
			adminService.delete(admin);
		}
	   resultView ="/admin/admin_queryList?dateList.pageNo=1";
		return "update";
	}
   /**
    * 分页查询
    * @return
    */
    @SuppressWarnings("unchecked")
	public String queryList(){
    	if(dataList==null){
    		dataList = new PageList<Admin>();	
    	}
    	dataList.setPageSize(15);
    	
    		int allrow = adminService.getAllRowCount("from  Admin", null);
    		dataList.setRowCount(allrow);
    		System.out.println(allrow);
    		dataList.setDataList(adminService.queryForPage(
    				dataList.getStartOfPage(), dataList.getEndRow(), "from Admin",null));
		
		return SUCCESS;
	}
    
   
    /**
     * 通过Id查询
     * @return
     */
   public String queryById(){
	   if(admin==null){
		   admin = new Admin();
		   admin.setId((Integer)session.get("nameId"));
		}	
	   admin = adminService.QueryById(Admin.class,admin.getId());
		return "edit";
	}
   /*相关参数注入
  	 *********************************************/
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	public AdminService getAdminService() {
		return adminService;
	}
	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}
	public String getResultView() {
		return resultView;
	}
	public void setResultView(String resultView) {
		this.resultView = resultView;
	}
	public PageList getDataList() {
		return dataList;
	}
	public void setDataList(PageList dataList) {
		this.dataList = dataList;
	}
	public Map getSession() {
		return session;
	}
	public void setSession(Map session) {
		this.session = session;
	}
   
  
   
    
}
